<?php
/**
 * Created by PhpStorm.
 * User: 123
 * Date: 2021/6/29
 * Time: 10:46
 */
namespace app\api\controller;

use app\api\service\JuHe;
use app\common\controller\Api;
use think\Request;

class Change extends Api
{
    protected $noNeedLogin = '';
    protected $noNeedRight = ['*'];
    public $juHe = '';

    public function __construct(Request $request = null)
    {
        parent::__construct($request);
        $this->juHe = new JuHe();
    }

    public function index()
    {
        $postData = json_decode(request()->getContent(), true);
        // 用户信息
        $postData['userInfo'] = $this->auth->getUserinfo();
        $postData['ip'] = request()->ip();

        // 数据验证
        $result = $this->validate($postData, 'Change');
        if (true !== $result) {
            // 验证失败 输出错误信息
            $this->error($result);
        }
        $data = $this->juHe->change($postData);
        if(!$data) $this->error($this->juHe->getError());

        $data['shortFile'] = strstr($data['file'], '/uploads');

        $this->success('变更成功', $data);
    }

    // 变更记录
    public function list()
    {
        $postData = json_decode(request()->getContent(), true);

        // 数据验证
        $result = $this->validate($postData, 'ChangeRecord');
        if (true !== $result) {
            // 验证失败 输出错误信息
            $this->error($result);
        }

        $data = \app\common\model\Change::getList($postData);
        $this->success('查询成功', $data);
    }
}